package com.qianfeng.smartdevice.service.impl;

import com.qianfeng.smartdevice.cache.AreaCache;
import com.qianfeng.smartdevice.constans.ResultCode;
import com.qianfeng.smartdevice.exceptions.QueryDataException;
import com.qianfeng.smartdevice.pojo.Area;
import com.qianfeng.smartdevice.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class AreaServiceImpl implements IAreaService {
    @Autowired
    private AreaCache areaCache;


    @Override
    public List<Area> getAllParentArea() {
        List<Area> allData = areaCache.getAllData();//所有的区域
        //需要获取父区域,就是看看它的 parent 是不是空
        List<Area> areaList = allData.stream().filter(
                //父区域 id 为空的就是一级父区域,所以我们过滤的时候很简单,只要判断 parentid==null
                area -> area.getParentid() == null

        ).collect(Collectors.toList());
        return areaList;
    }

    @Override
    public List<Area> getAllAreasByParentId(Long parentId) {
        if (parentId == null || parentId <= 0) {
            throw  new QueryDataException("主键不存在", ResultCode.DATA_NOT_EXIST);
        }
        List<Area> allData = areaCache.getAllData();//所有的区域
        //获取parentsid==我们传递的 id 数据
        List<Area> areaList = allData.stream().filter(
                area -> area.getParentid() == parentId

        ).collect(Collectors.toList());
        return areaList;
    }


}
